package com.crm.service.impl.cluePoolManager;

import com.alibaba.fastjson.JSONObject;
import com.crm.dao.mapper.cluePoolManager.ClueReturnMapper;
import com.crm.dao.mapper.customMapper.cluePoolManagerCustomMapper.ClueReturnCustomMapper;
import com.crm.model.entity.cluePoolManager.ClueReceiveExample;
import com.crm.model.entity.cluePoolManager.ClueReturn;
import com.crm.model.entity.cluePoolManager.ClueReturnExample;
import com.crm.model.qo.cluePoolManager.ClueQO;
import com.crm.model.vo.cluePoolManager.ClueReceiveVO;
import com.crm.model.vo.cluePoolManager.ClueReturnVO;
import com.crm.service.cluePoolManager.ClueReturnDbService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @ Author ：pym
 * @ Date   ：2021/1/8 13:03
 */
@Service
public class ClueReturnDbServiceImpl implements ClueReturnDbService {
    @Autowired
    ClueReturnCustomMapper clueReturnCustomMapper;
    @Autowired
    ClueReturnMapper clueReturnMapper;

    /**
     * 线索池详情-线索清单-退回记录
     * @param clueId        ---线索Id
     * @return
     */
    @Override
    public List<ClueReturnVO> selectClueReturnVOList(String clueId) {
        List<ClueReturnVO> clueReturnVOList = clueReturnCustomMapper.selectClueReturnVOList(clueId);
        return clueReturnVOList;
    }

    /**
     * 新增一条线索退回记录表
     * @param clueReturn
     * @return
     */
    @Override
    public int insertClueReturn(ClueReturn clueReturn) {
        return clueReturnMapper.insert(clueReturn);
    }

    /**
     * @author wll
     * info 同一线索释放或者收回的最新一条记录信息
     */
    @Override
    public ClueReturn selectLatestClueReturnOneInfo(ClueQO clueQO) {
        ClueReturnExample clueReturnExample = new ClueReturnExample();
        ClueReturnExample.Criteria criteria = clueReturnExample.createCriteria();
        criteria.andClueIdEqualTo(clueQO.getClueId());
        clueReturnExample.setOrderByClause("return_tm desc");
        List<ClueReturn> clueReturns = clueReturnMapper.selectByExample(clueReturnExample);
        if(null != clueReturns && clueReturns.size()>0){
            return clueReturns.get(0);
        }
        return null;

    }


    /**
     * @author wll
     * info 同一线索是否满足(释放 收回)时间间隔  小时
     */
    @Override
    public JSONObject selectTheSameThatClueTimeInterval(Integer timeInterval, ClueReturn clueReturn) {
        JSONObject result = new JSONObject();
        if(null == clueReturn){
            result.put("status",true);
            return result;
        }else{
            Date returnTm = clueReturn.getReturnTm();
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(returnTm);
            calendar.add(Calendar.HOUR, timeInterval);
            returnTm = calendar.getTime();
            Date current_time = new Date();
            if(returnTm.before(current_time)){
                result.put("status",true);
                return result;
            }else{
                result.put("status",false);
                result.put("toast","该条线索暂不能领取");
                return result;
            }
        }
    }

    /**
     *(线索清单)线索退回次数
     * @param clueId ---线索ID
     * @return
     */
    public long selectReturnCountByClueId(String clueId){
        return clueReturnCustomMapper.selectReturnCountByClueId(clueId);
    }
}
